package com.wuwei.tms.setting.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuwei.tms.pojo.Airplane;
import com.wuwei.tms.pojo.Tool;
import com.wuwei.tms.setting.mapper.AirplaneMapper;
import com.wuwei.tms.setting.mapper.UserMapper;
import com.wuwei.tms.setting.service.AirplaneService;
import com.wuwei.tms.utils.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value="/T01/airplane")
//@CrossOrigin
public class AirplaneController {

    @Resource
    private AirplaneService airplaneService;

    @Resource
    private AirplaneMapper airplaneMapper;

    @Resource
    private UserMapper userMapper;

    @PostMapping("/add")
    public Result<String> add (@RequestBody Airplane airplane){
        //这里新增的工具的barcode不能重复
        LambdaQueryWrapper<Airplane> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Airplane::getAirplane,airplane.getAirplane());
        if (airplaneMapper.selectCount(queryWrapper)!=0) {
            return Result.fail("飞机号重复！");
        }
        return airplaneService.addAirplane(airplane);
    }

    @PostMapping("/update")
    public Result<String> update (@RequestBody Airplane airplane){
        LambdaQueryWrapper<Airplane> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Airplane::getAirplane,airplane.getAirplane());
        airplaneMapper.updateById(airplane);
        return Result.ok("更新成功");
    }

    @PostMapping("/delete/{id}")
    public Result<String> update (@PathVariable("id") Long id){
        airplaneMapper.deleteById(id);
        return Result.ok("删除成功");
    }


    @GetMapping("list/{currentPage}/{pageSize}/{search}")
    /**
     * 得到所有的飞机传给前端
     */
    public Result<List<Airplane>> list (@PathVariable("currentPage") Integer currentPage , @PathVariable("pageSize") Integer pageSize, @PathVariable ("search") String search)  {
        if ("_".equals(search)) {
            search="";
        }
        List<Airplane> airplanes = airplaneService.listAndPagination(currentPage,pageSize,search);
        return Result.ok(airplanes);
    }


    @GetMapping("total/{search}")
    /**
     * 得到所有的飞机传给前端
     */
    public Result<Long> total (@PathVariable ("search") String search) {
        if ("_".equals(search)) {
            search="";
        }
        LambdaQueryWrapper<Airplane> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Airplane::getAirplane,search);
        Long count = airplaneMapper.selectCount(queryWrapper);
        return Result.ok(count);
    }

    @GetMapping("all")
    /**
     * 得到所有的飞机传给前端
     */
    public Result<List<Airplane>> all ()   {
        LambdaQueryWrapper<Airplane> queryWrapper = new LambdaQueryWrapper<>();
        List<Airplane> airplanes = airplaneMapper.selectList(queryWrapper);
        return Result.ok(airplanes);
    }

}
